#include "StdAfx.h"
#include "AnyDocument.h"
#include "TDocStd_Document.hxx"
#include "IDocListener.h"
#include "EditorData.h"
#include "LabelTools.h"
#include "UndoCommand.h"
//#ifdef _DEBUG
	#include "DebugBrowser.hxx"
	#pragma  comment(lib, "DFBrowser.lib")
//#endif

CAnyDocument::CAnyDocument(Handle_TDocStd_Document hDoc)
:m_hOcafDoc(hDoc),
m_pUodoManager(NULL)
{
	m_pUodoManager = new CUndoManager;
}

CAnyDocument::~CAnyDocument(void)
{
	delete m_pUodoManager;
}
void CAnyDocument::NewDocument(const CString& szName)
{
	m_szDocName = szName;
	DocListenerList::iterator it = m_listListeners.begin();
	for(; it != m_listListeners.end(); ++it)
	{
		(*it)->OnNewDocument(this);
	}

}
void CAnyDocument::CloseDocument()
{
	DocListenerList::iterator it = m_listListeners.begin();
	for(; it != m_listListeners.end(); ++it)
	{
		(*it)->OnCloseDocument(this);
	}
}

void CAnyDocument::SetActive(bool bActive)
{
	DocListenerList::iterator it = m_listListeners.begin();
	for(; it != m_listListeners.end(); ++it)
	{
		(*it)->OnActiveDocument(this, bActive);
	}
}
TDF_Label CAnyDocument::GetRootLabel()
{
	return m_hOcafDoc->Main();
}

void CAnyDocument::SetUndoLimit(UINT nLimit)
{
	m_hOcafDoc->SetUndoLimit(nLimit);
}


void CAnyDocument::BrowserTree()
{
//#ifdef _DEBUG
	DebugBrowser::DFBrowser(m_hOcafDoc);
//#endif
}

bool CAnyDocument::AddShape(TDF_Label& subLabel, CEditorData& edData)
{

	ItemTrait itemTrait;
	TDF_Label childLabel;
	LabelTools::GetChild(subLabel, itemTrait.szName,childLabel);
	itemTrait.hIcon = edData.hIcon;
	itemTrait.nEditorId = edData.nID;

	DocListenerList::iterator it = m_listListeners.begin();
	for(; it != m_listListeners.end(); ++it)
	{
		(*it)->OnAddShape(this, itemTrait);
	}
	return true;
}

bool CAnyDocument::AddListener(IDocListener* pDocListener)
{
	m_listListeners.push_back(pDocListener);
	return true;
}

bool CAnyDocument::RemoveListener(IDocListener* pDocListener)
{
	m_listListeners.remove(pDocListener);
	return true;
}